# == Schema Information
#
# Table name: product_cases
#
#  id          :integer          not null, primary key
#  title       :string
#  deleted_at  :datetime
#  desc        :text
#  product_ids :string           default([]), is an Array
#  created_at  :datetime         not null
#  updated_at  :datetime         not null
#

class ProductCase < ApplicationRecord
  include ActsAsParanoidConcern

  validates :title, presence: { message: '标题不能为空' }
  validates :desc, presence: { message: '描述不能为空' }
  validates :category_id, presence: { message: '分类不能为空' }
  validates :brand_id, presence: { message: '品牌不能为空' }
  validates :product_ids, presence: { message: '关联商品不能为空' }

  has_many :product_case_attachments


  def image
    self.product_case_attachments.first.image_url
  end

  def products
    Product.unscoped.find(self.product_ids)
  end

  def images
    self.product_case_attachments.map{|item| item.image_url}
  end

  def as_json(options={})
    super({include: [], methods: [:images, :products]}.merge(options))
  end
end
